#!/usr/bin/perl
########################################################
# standard stuff
my $TargetDisk				= $ARGV[2];
my $SystemVersionPlist		= $TargetDisk . "/System/Library/CoreServices/SystemVersion.plist";
my $GAS_Framework			= "/System/Library/PrivateFrameworks/GraphicsAppSupport.framework/Frameworks/";


# Do nothing if target disk is Tiger
exit 0 if CheckVersion($SystemVersionPlist, "10.5", "ProductVersion", "<");

# Do nothing if there's no GAS_Framework
exit 0 if (!-e $GAS_Framework);

# There are two symlinks in the GAS_Framework that might need repair
# We will delete whatever's there and create a fresh symlink

# ImageKit
my $ImageKitLink			= "ImageKit.framework";
my $ImageKitTarget			= "../../../../../Frameworks/Quartz.framework/Versions/A/Frameworks/ImageKit.framework";
repairLink($GAS_Framework, $ImageKitLink, $ImageKitTarget);

# QuartzComposer
my $QuartzComposerLink		= "QuartzComposer.framework";
my $QuartzComposerTarget	= "../../../../../Frameworks/Quartz.framework/Versions/A/Frameworks/QuartzComposer.framework";
repairLink($GAS_Framework, $QuartzComposerLink, $QuartzComposerTarget);

########################################################
# always exit 0
exit 0;














########################################################
########################################################
########################################################
########################################################
########################################################
########################################################
########################################################
########################################################
########################################################

########################################################
sub repairLink
{
    my $directory	= $_[0];
    my $link		= $_[1];
    my $target		= $_[2];
	
	# chdir to the directory
	chdir $directory;
	
	# Delete whatever is where the link is
	deleteTree($link);
	
	# Now make a link there
	symlink $target, $link;
	
}

########################################################
sub deleteTree
{
    my $path            = $_[0];

    if (-e $path)
    {
        if (-d $path
	    && !(-l $path))
        {
            local* THEDIR;
            my $file;

            opendir THEDIR, $path;

            while ($file = readdir THEDIR)
            {
				if ($file ne '.' && $file ne '..')
				{
					deleteTree($path . "/" . $file);
				}
            }

            closedir THEDIR;

	    rmdir $path;
        } else {

            unlink $path;
        }
    } else {
        if (-l $path)
        {
            unlink $path;
        }
    }
}

########################################################
sub CheckVersion
{
    my $path            = $_[0];
    my $version         = $_[1];
    my $keyName         = $_[2];
    my $operator        = $_[3];
    my $i;
    my $oldSeperator;
    my $plistData;
    my @items;
    my $item;
    my @theVersionArray;
    my @versionArray;
    my %versiondata;

    if (! -e $path) {
        return 0;
    }

    if (!$operator) {
        $operator = "==";
    }

    $oldSeperator = $/;
    $/ = \0;

    open( PLIST, "$path") || do {
        return 0;
    };

    $plistData = <PLIST>;
    $plistData =~ /<dict>(.*?)<\/dict>/gis;

    @items = split(/<key>/, $plistData);

    shift @items;
    foreach $item (@items) {
        $item =~ /(.*?)<\/key>.*?<string>(.*?)<\/string>/gis;
        $versiondata{ $1 } = $2;
    }

    close(PLIST);

    $/ = $oldSeperator;

    @theVersionArray = split(/\./, $versiondata{$keyName});
    for ($i = 0; $i < 3; $i++) {
        if(!$theVersionArray[$i]) {
            $theVersionArray[$i] = '0';
        }
    }

    @versionArray = split(/\./, $version);
    
    my $actualVersion;

    for ($i = 0; $i < 3; $i++) {
        if (($theVersionArray[$i] != $versionArray[$i]) or ($i == 2)) {

            $actualVersion = $theVersionArray[$i];
            $version = $versionArray[$i];

            last;
        }
    }

    my $expression = '$actualVersion ' . $operator . ' $version';
    if( eval ($expression) )
    {
        return 1;
    }
    else
    {
        return 0;
    }

}
